package com.zcgame.customview.fresh

import android.view.View

/**
 *@author:huangLJ
 *@date:2021/5/10 18:16
 */
class ProxyPullHeader(private var pullHeader: PullHeader):PullHeader {

    private val listeners = HashSet<OnPullListener>(3)


    override fun createHeaderView(refreshLayout: PullRefreshLayout): View {
        return pullHeader.createHeaderView(refreshLayout)
    }

    override fun getConfig(): PullHeader.HeaderConfig {
        return pullHeader.getConfig()
    }

    override fun needCoverContent(): Boolean {
        return pullHeader.needCoverContent()
    }

    override fun onPullBegin(refreshView: PullRefreshLayout) {
        pullHeader.onPullBegin(refreshView)
        listeners.forEach{ listener->
            listener.onPullBegin(refreshView)
        }
    }

    override fun onPositionChange(refreshView: PullRefreshLayout, status: Byte, dy: Int, currentDistance: Int) {
        pullHeader.onPositionChange(refreshView, status, dy, currentDistance)
        listeners.forEach{ listener->
            listener.onPositionChange(refreshView, status, dy, currentDistance)
        }
    }

    override fun onRefreshing(refreshView: PullRefreshLayout) {
        pullHeader.onRefreshing(refreshView)
        listeners.forEach{ listener->
            listener.onRefreshing(refreshView)
        }
    }

    override fun onReset(refreshView: PullRefreshLayout, pullRelease: Boolean) {
        pullHeader.onReset(refreshView,pullRelease)
        listeners.forEach{ listener->
            listener.onReset(refreshView,pullRelease)
        }
    }

    override fun onPullRefreshFinish(pullRefreshLayout: PullRefreshLayout) {
        pullHeader.onPullRefreshFinish(pullRefreshLayout)
        listeners.forEach{ listener->
            listener.onPullRefreshFinish(pullRefreshLayout)
        }
    }


    fun setPullHandler(pullHeader: PullHeader) {
        this.pullHeader = pullHeader
    }

    fun addHeaderListener(listener: OnPullListener) {
        if(!listeners.contains(listener)){
            listeners.add(listener)
        }
    }
    fun removeHeaderListener(listener:OnPullListener){
        if (listeners.contains(listener)) {
            listeners.remove(listener)
        }
    }
}